package com.unique.demo.controller;

import java.util.List;

import org.unique.commons.utils.DateUtil;
import org.unique.ioc.annotation.Autowired;
import org.unique.web.annotation.Controller;
import org.unique.web.annotation.Path;
import org.unique.web.annotation.Path.ResponseType;
import org.unique.web.annotation.PathParam;
import org.unique.web.core.R;
import org.unique.web.rest.ResponseBody;

import com.unique.demo.model.Person;
import com.unique.demo.service.PersonService;

/**
 * 测试控制器
 * @author biezhi
 * @version 1.0
 */
@Controller
public class IndexController {
	
	@Autowired
	private PersonService personService;
	
	@Path
	public String index(){
		R.setAttr("username", "王爵");
		return "index";
	}
	
	/**
	 * hello控制器
	 * @return
	 */
	@Path
	public String hello(){
		System.out.println("我是hello");
		return "hello";
	}
	
	/**
	 * 保存数据
	 */
	@Path
	public void save(){
		String name = R.getPara("name");
		Integer age = R.getParaToInt("age");
		String birthdate = R.getPara("birthday");
		Integer birthday = DateUtil.convertStringDateToInt(birthdate, "yyyy-MM-dd");
		System.out.println("name=" + name);
		System.out.println("age=" + age);
		System.out.println("birthdate=" + birthdate);
		
		boolean flag = personService.save(name, age, birthday, 1);
		R.renderText("{result : " + flag + "}");
	}
	
	@Path( value = "/show/:id", response = ResponseType.JSON)
	public ResponseBody show(@PathParam Integer id){
		System.out.println("id = " + id);
		//Person person = personService.get(id);
		//R.setAttr("person", person);
		return R.response(this, "aaa");
	}
	
	@Path
	public void update(){
		
	}
	
	@Path
	public String persons(){
		String name = R.getPara("name");
		Integer status = R.getParaToInt("status");
		List<Person> personList = personService.getList(name, status);
		R.setAttr("personList", personList);
		return "list";
	}
	
	@Path
	public void delete(){
		
	}
	
}
